package com.shoppingmall.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;

import com.example.shoppingmall.R;
import com.shopping.tools.CategoryListTools;
import com.shopping.tools.Constant;
import com.shopping.tools.GetUrlMethod;
import com.shoppingmall.Adapter.HomePromoteActivityAdapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * 商家类别Activity
 * 
 */
public class ShopTypeActivity extends Activity implements OnClickListener {
	private TextView tv_shopTypeTitle;
	private ImageView img_shopTypeback;
	private ArrayList<HashMap<String, String>> shopTypeList = new ArrayList<HashMap<String, String>>();
	private ShopTypeThread mShopTypeThread;
	private String str_req = "";
	private ListView lv_shopType;
	private ProgressDialog progressDialog;
	private HomePromoteActivityAdapter mHomePromoteActivityAdapter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.shoptypelist);
		init();
		mHomePromoteActivityAdapter = new HomePromoteActivityAdapter(
				ShopTypeActivity.this, shopTypeList);
		lv_shopType.setAdapter(mHomePromoteActivityAdapter);
		mShopTypeThread = new ShopTypeThread();
		mShopTypeThread.start();// 开启mShopTypeThread线程

	}

	private void init() {
		// TODO Auto-generated method stub
		progressDialog = ProgressDialog.show(ShopTypeActivity.this, "", "正在加载中。。。",
				true, true);
		lv_shopType = (ListView) findViewById(R.id.lv_shopType);
		tv_shopTypeTitle = (TextView) findViewById(R.id.tv_shopTypeTitle);
		tv_shopTypeTitle.setText(Constant.shopTypeTitle);// 设置商店标题
		img_shopTypeback = (ImageView) findViewById(R.id.img_shopTypeback);
		img_shopTypeback.setOnClickListener(this);
	}

	private class ShopTypeThread extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			str_req = "{\"mallId\":1,\"shopTypeId\":" + Constant.shopTypeId
					+ "}";// 照片墙详情参数
			String urlPath;
			try {
				urlPath = GetUrlMethod.getURLString(Constant.shopTypeurl
						+ URLEncoder.encode(str_req, "utf-8"));
				System.out.println("------商店数据----" + urlPath);
				CategoryListTools.json_PromoteActivity(urlPath, shopTypeList);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Message msg = new Message();
			msg.what = 0;
			myhandler.sendMessage(msg);// 向myhandler发送消息
		}
	};

	@SuppressLint("HandlerLeak")
	private Handler myhandler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				mHomePromoteActivityAdapter.notifyDataSetChanged();
				progressDialog.dismiss();
				break;
			}
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.img_shopTypeback:// 点击返回
			this.finish();
			break;
		}
	}

}
